package com.bap.ele.workbench.ui.views;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.IViewPart;

import com.bap.ele.workbench.ui.views.model.CourseTreeNode;
import com.bap.ele.workbench.ui.views.model.IContentNode;

public class CourseContentProvider implements IStructuredContentProvider,
		ITreeContentProvider {

	private IContentNode invisibleRoot;

	private IViewPart view;

	public CourseContentProvider(IViewPart view) {
		super();
		this.view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
	 * .lang.Object)
	 */
	public Object[] getElements(Object inputElement) {
		if (inputElement.equals(view.getViewSite())) {
			if (invisibleRoot == null)
				initialize();
			return getChildren(invisibleRoot);
		}
		return getChildren(inputElement);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public void dispose() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
	 * .viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.
	 * Object)
	 */
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof IContentNode) {
			return ((IContentNode) parentElement).getChildren();
		}
		return new Object[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object
	 * )
	 */
	public Object getParent(Object element) {
		if (element instanceof IContentNode) {
			return ((IContentNode) element).getParent();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.
	 * Object)
	 */
	public boolean hasChildren(Object element) {
		if (element instanceof IContentNode) {
			return ((IContentNode) element).hasChildren();
		}
		return false;
	}

	private void initialize() {
		invisibleRoot = new CourseTreeNode("");

		System.out.println("--start getting object from workspace--");
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot workSpaceRoot = workspace.getRoot();

		try {
			for (IResource resource : workSpaceRoot.members()) {
				System.out.println("---" + resource.getName());
				if (resource instanceof IContainer) {
					IContentNode obj = createContent((IContainer) resource);
					invisibleRoot.addChild(obj);
					obj.setParent(invisibleRoot);
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
		System.out.println("--end getting object from workspace--");
	}

	private IContentNode createContent(IContainer resource)
			throws CoreException {
		IContentNode course = new CourseTreeNode(resource.getName());
		for (IResource child : resource.members()) {
			IContentNode obj = null;
			if (child instanceof IContainer) {
				obj = createContent((IContainer) child);
			}else{
				obj = new CourseTreeNode(child.getName());
			}
			course.addChild(obj);
			obj.setParent(course);
		}
		return course;
	}

}
